package cn.ai.boot.config;

/**
 * @author jie.zhao@dms365.com
 * @Date 2025/8/22 15:41
 */

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 计数器
 */

import java.time.Duration;
import java.time.LocalTime;


import java.time.Duration;
import java.time.LocalTime;

public class OvertimeCalculator {
    // 标准工作时间
    private static final LocalTime WORK_START = LocalTime.of(9, 0);
    private static final LocalTime WORK_END = LocalTime.of(18, 30);
    private static final Duration TARGET_OVERTIME = Duration.ofHours(6);

    // 周一到周四工作时间记录（上班时间，下班时间）
    private static final LocalTime[][] WEEK_SCHEDULE = {
            {LocalTime.of(8, 46), LocalTime.of(19, 48)},  // 周一
            {LocalTime.of(8, 52), LocalTime.of(18, 27)},   // 周二
            {LocalTime.of(8, 51), LocalTime.of(20, 55)},   // 周三
            {LocalTime.of(8, 53), LocalTime.of(20, 0)},  // 周四
    };

    // 周五上班时间
    private static final LocalTime FRIDAY_START = LocalTime.of(8, 46);

    public static void main(String[] args) {
        String[] weekDays = {"周一", "周二", "周三", "周四"};
        Duration totalOvertime = Duration.ZERO;

        // 计算并输出每日加班时长
        for (int i = 0; i < 4; i++) {
            Duration dailyOvertime = calculateDailyOvertime(WEEK_SCHEDULE[i]);
            System.out.printf("%s 加班时长: %s%n",
                    weekDays[i], formatDuration(dailyOvertime));
            totalOvertime = totalOvertime.plus(dailyOvertime);
        }

        // 计算周五需补足时间
        Duration remainingOvertime = TARGET_OVERTIME.minus(totalOvertime);

        if (remainingOvertime.isNegative()) {
            System.out.printf("本周已加班%s，已超过目标6小时%n", formatDuration(totalOvertime));
        } else {
            LocalTime requiredLeaveTime = WORK_END.plus(remainingOvertime);
            System.out.println("本周总加班: " + formatDuration(totalOvertime));
            System.out.println("周五需加班: " + formatDuration(remainingOvertime));
            System.out.println("最晚下班时间: " + requiredLeaveTime);
        }
    }

    private static Duration calculateDailyOvertime(LocalTime[] daySchedule) {
        Duration overtime = Duration.ZERO;

        // 计算早到时间
        Duration earlyArrival = Duration.between(daySchedule[0], WORK_START);
        if (!earlyArrival.isNegative()) {
            overtime = overtime.plus(earlyArrival);
        }

        // 计算加班时间
        Duration lateDeparture = Duration.between(WORK_END, daySchedule[1]);
        if (!lateDeparture.isNegative()) {
            overtime = overtime.plus(lateDeparture);
        }

        return overtime;
    }

    private static String formatDuration(Duration duration) {
        long hours = duration.toHours();
        long minutes = duration.minusHours(hours).toMinutes();
        return String.format("%d小时%d分钟", hours, minutes);
    }
}

